#include "StdFile.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "DoubleLinkList.h"
#define true 1
#define false 0
int IsFileExist(const char *filepath)
{
    FILE *file=fopen(filepath,"r");
    if(file!=NULL)
    {
        fclose(file);
        return true;
    }
    else
    {
        return false;
    }
}

void CreateFile(const char *filepath)
{
    FILE *file=fopen(filepath,"r");
    if(file!=NULL)
    {
        printf("file is exist\n");
        fclose(file);
        return ;
    }
    file=fopen(filepath,"w");
    fclose(file);
}

void CreateFloder(const char *floderpath)
{
    int result=mkdir(floderpath,0777);
    if(result==0)
    {
        printf("create floder success\n");
    }
    else
    {
        printf("create floder error\n");
    }
}

char *LoadFromFile(const char *filepath)
{
    FILE *file=fopen(filepath,"r");
    if(file==NULL)
    {
        printf("open file error\n");
        return NULL;
    }
    fseek(file,0,SEEK_END);
    long size=ftell(file);
    char *s=(char*)malloc(size);
    if(s==NULL)
    {
        printf("LoadFromFile malloc error\n");
        fclose(file);
        return NULL;
    }
    fseek(file,0,SEEK_SET);
    fread(s,size,1,file);
    fclose(file);
    return s;
}

DLList *GetLineFromFile(const char *filepath)
{
    FILE *file=fopen(filepath,"r");
    if(file==NULL)
    {
        printf("open file error\n");
        return NULL;
    }
    DLList *list=(DLList *)malloc(sizeof(DLList));
    if(list==NULL)
    {
        printf("list malloc error\n");
        return NULL;
    }
    InitDLinkList(list);
    char temp[100]={0};
    while(fgets(temp,100,file)!=NULL)
    {
        if(strcmp(temp,"\n")==0)
            continue;
        char *linecontent=(char*)malloc(sizeof(char)*(strlen(temp)+1));
        strcpy(linecontent,temp);
        InsertTail(list,linecontent);
    }
    fclose (file);
    return list;
}

void SaveToFile(const char *filepath, void *ptr, size_t size)
{
    FILE *file=fopen(filepath,"w");
    if(file==NULL)
    {
        printf("open file error\n");
        return ;
    }
    fwrite(ptr,size,1,file);
    fclose(file);
}

void SaveLineToFile(const char *filepath, DLList *list)
{
    FILE *file=fopen(filepath,"w");
    if(file==NULL)
    {
        printf("open file error\n");
        return ;
    }
    Node *TravelPoint=list->head;
    while(TravelPoint!=NULL)
    {
        fputs(TravelPoint->data,file);
        TravelPoint=TravelPoint->next;
    }
    fclose(file);
}
void RemoveFile(const char *filepath)
{
    char temp[100]={0};
    sprintf(temp,"rm -f %s",filepath);
    system(temp);
}
void AppendToFile(const char *filepath, void *ptr)
{
    FILE *file=fopen(filepath,"a");
    if(file==NULL)
    {
        printf("open file error\n");
        return ;
    }
    fputs(ptr,file);
    fclose(file);
}

void CopyFile(const char *oldpath, const char *newpath)
{
    if(IsFileExist(oldpath)==false)
    {
        printf("copy is not exist\n");
        return;
    }
    char *content=LoadFromFile(oldpath);
    if(IsFileExist(newpath)==true)
    {
        char temp[100]={0};
        char temp2[100]={0};
        strcpy(temp2,newpath);
        char *filename=strtok(temp2,".");
        char *backname=strtok(NULL,".");
        strcat(temp,filename);
        strcat(temp,"_new.");
        strcat(temp,backname);
        SaveToFile(temp,content,strlen(content));
    }
    SaveToFile(newpath,content,strlen(content));
    free(content);
}

